import {
	getToken,
	removeToken
} from './ahth.js'
// let baseUrl = 'http://192.168.1.200:8083/api/'
// let baseUrl = 'https://sqtg.soft.haoyangsoft.com/api/'   //社区团购地址
let baseUrl = 'https://jl.soft.haoyangsoft.com/api/' //好样团购
// let baseUrl = 'https://d1.sqtg.junwoshengwu.com/api/' //伊亚塔西餐厅
async function request(mehtod, params, type, callBack) { //创建一个名为request请求的方法函数
	console.log('zhixingle')
	// 参数分别为: 接口名称、参数、回调函数
	// ------在这里设置一些默认属性值-----
	let forceLogin = false //是否强制登录

	if (!type) {
		type = 'GET'
	}
	console.log(params)
	let header = { //设置请求头信息
		'Authorization': 'asdfgwe'+getToken(),
		'X-Requested-With': 'XMLHttpRequest',
		"Accept": "application/json",
		"Content-Type": "application/json; charset=UTF-8"
	};
	let http = {
		url: baseUrl + mehtod,
		data: params,
		method: type,
		header: header
	}
	console.log('token', getToken())
	let promise = new Promise((resolve, reject) => {
		uni.request(http)
			.then(res => {
				let newdata = JSON.parse(res[1].data)
				if (newdata.code == 403) {
					removeToken()
					if (forceLogin) { //强制登录，直接跳到登录页
						uni.navigateTo({
							url: '/pages/login/login.vue'
						})
					} else { //弹出提示框，点击确定去登录
						uni.showModal({
							title: "立即登录",
							content: "该功能需要登录！",
							success: (result) => {
								if (result.confirm) {
									uni.navigateTo({
										url: '/pages/login/login.vue'
									})
								}
							}
						})
					}
				}
				if(newdata == -1){ //如果错误码为 -1 提示
					uni.showToast({
						title:res[1].data.msg,
						icon:'none'
					})
				}
				resolve(JSON.parse(res[1].data))
			}).catch(err => {
				reject(err)
				console.log(err)
			})
	})
	return promise
}
export default {
	request
}
